package com.example.drss.fragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.drss.DRSSActivity;
import com.example.drss.DishesActivity;
import com.example.drss.R;
import com.example.drss.asynctask.MyAsyncTask;
import com.example.drss.bean.Weather;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class OneFragment extends Fragment {
	View view;	
	final static String URL="http://www.weather.com.cn/adat/cityinfo/101070201.html";
	TextView weatherTextView;
	DRSSActivity activity;
	
	ArrayList<View> views = new ArrayList<View>();
	ArrayList<String> titles = new ArrayList<String>();
	
	PagerTabStrip pts = null;
	ViewPager viewPager = null;
	
	boolean is = true ;
	Thread autoChangeThread = null;
	Handler handler = null;
	

	private void execAsyncTask(String url){
		MyAsyncTask task = new MyAsyncTask(OneFragment.this);
		task.execute(URL);
	}
	
		private Weather parseJSON(String result){
			Weather weather;

			try {
				weather=new Weather();
				JSONObject weatherObject=new JSONObject(result).getJSONObject("weatherinfo");
						String cityid=weatherObject.getString("cityid");
						String city=weatherObject.getString("city");
						String temp1=weatherObject.getString("temp1");
						String weather1=weatherObject.getString("weather");
						String temp2=weatherObject.getString("temp2");
						weather.setCityid(cityid);
						weather.setCity(city);
						weather.setTemp1(temp1);
						weather.setWeather(weather1);
						weather.setTemp2(temp2);

			} catch (JSONException e) {
				e.printStackTrace();
				return null;
			}
			return weather ;
		}
		
		public void updateUI(String result){
			if(result!=null){
				Weather weather= parseJSON(result);
				SimpleDateFormat    formatter    =   new    SimpleDateFormat    ("HH");       
				Date    curDate    =   new    Date(System.currentTimeMillis());//获取当前时间 
				String    str    =    formatter.format(curDate);
				if(Integer.parseInt(str)>=5&&Integer.parseInt(str)<=10){
					str="早上好";
				}else if (Integer.parseInt(str)<13&&Integer.parseInt(str)>10) {
					str="中午好";
				}else if (Integer.parseInt(str)<17&&Integer.parseInt(str)>=13) {
					str="下午好";
				}
				else if (Integer.parseInt(str)<23&&Integer.parseInt(str)>=17) {
					str="晚上好";
				}
				else  {
					str="现在是睡觉时间哦";
				}
				weatherTextView.setText(str+"\n"+weather.getCity()+"天气："+weather.getWeather()+
						"\n气温:"+weather.getTemp2()+"~"+weather.getTemp1());
			}else{
				Toast.makeText(activity, "未联网！", Toast.LENGTH_SHORT).show();
			}
		}

	
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		view=inflater.inflate(R.layout.fragment_one, container, false);	
		
		activity=(DRSSActivity)getActivity();
		
		weatherTextView=(TextView)view.findViewById(R.id.textView_weather);
		execAsyncTask(URL);		
		
		LayoutInflater inflater1 = LayoutInflater.from(activity);
		
		View view1 = inflater1.inflate(R.layout.activity_image, null);
		ImageView imageView = (ImageView) view1.findViewById(R.id.my_iv);
		imageView.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.image14));
		
		View view2 = inflater1.inflate(R.layout.activity_image, null);
		ImageView imageView2 = (ImageView) view2.findViewById(R.id.my_iv);
		imageView2.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.image3));
		
		View view3 = inflater1.inflate(R.layout.activity_image, null);
		ImageView imageView3 = (ImageView) view3.findViewById(R.id.my_iv);
		imageView3.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.image9));

		
		pts = (PagerTabStrip) view.findViewById(R.id.my_pts);
		pts.setDrawFullUnderline(false);
		
		views.add(view1);
		views.add(view2);
		views.add(view3);
		
		view1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(activity,DishesActivity.class);
				intent.putExtra("name", "熏肉大饼");
				intent.putExtra("databasePath_dishes",activity.databasePath);
				startActivity(intent);
			}
		});
		view2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(activity,DishesActivity.class);
				intent.putExtra("name", "土豆泥");
				intent.putExtra("databasePath_dishes",activity.databasePath);
				startActivity(intent);
			}
		});
		view3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(activity,DishesActivity.class);
				intent.putExtra("name", "排骨米粉");
				intent.putExtra("databasePath_dishes",activity.databasePath);
				startActivity(intent);
			}
		});
		
		titles.add("熏肉大饼");
		titles.add("土豆泥");
		titles.add("排骨米粉");
		
		viewPager = (ViewPager) view.findViewById(R.id.my_vp);
		viewPager.setAdapter(new MyAdapter());
		
		//viewPager.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
		
		//--------- viewpager循环滑动---------
		handler = new Handler(){
			public void handleMessage(Message msg) {
				if(msg.what == 1){
					viewPager.setCurrentItem(msg.arg1);
				}
				
			};
			
		};
		
		 autoChangeThread = new Thread(){
			public void run() {
				int p = 0;
				while(is){
				try {
					Thread.sleep(3000);
					if(p>3){
						p = 0 ;
					}
					Message message = new Message();
					message.what = 1;
					message.arg1 = p ;
					handler.sendMessage(message);
					
					p ++;
					
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				}
			};
		};
		autoChangeThread.start();
		//---------------------------
		
		
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				Log.i("TAG", ""+titles.get(arg0));
				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		return view;
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		activity.getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	class MyAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return views.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}
		
		/**
		 * 相当于是BaseAdapter中的getView方法。
		 * 在这个方法中需要返回视图对象，并且要把视图对象加入ViewGroup当中
		 */
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(views.get(position));
			
			return views.get(position) ;
		}
		
		
		/**
		 * 从ViewGroup当中，删除看不见的视图
		 */
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(views.get(position));
		
		}
		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return titles.get(position);
		}
	}
}
